package chai.ningning.creation;

import java.time.LocalDateTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 1、java.util.concurrent.Executor 实例用来执行已提交的Runnable任务
 * 2、java.util.concurrent.Executor 接口中声明的 execute（Runnable）用来执行线程任务
 * 3、java.util.concurrent.ExecutorService接口继承了java.util.concurrent.Executor接口
 * 4、java.util.concurrent.Executors类是用来支持ExecutorService相关操作的一个工具类
 */
public class ExecutorTest1 {

    public static void main(String[] args) {

        //java.util.concurrent.Executors类是用来支持ExecutorService相关操作的一个工具类
        //调用Executors中的类方法来创建一个线程池
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        System.out.println(executorService);

        //通过创建Runnable 实例来指定一个线程任务
        Runnable runnable = new Runnable() {
            @Override
            public void run() { //这个run方法中要完成的操作就是所谓的任务(task)
                System.out.println(Thread.currentThread());
                System.out.println("现在时间是"+ LocalDateTime.now());
            }
        };

        //java.util.concurrent.ExecutorService接口继承了java.util.concurrent.Executor接口
        //向线程池提交需执行的任务（task）
        executorService.execute(runnable);//在未来某个时间执行给定的命令

        //线程池的状态
        System.out.println(executorService);
        //关闭线程池
        executorService.shutdown();
    }
}
